<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addForm" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import treeTools from './components/tree-tools.vue'
import { tranListToTreeDate } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      node: {},
      company: {},
      departs: [],
      showDialog: false,
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      this.company = {
        name: res.companyName,
        manager: '负责人',
        id: ''
      }
      this.departs = tranListToTreeDate(res.depts, '')
    },
    addDepts(node) {
      this.showDialog = true
      // 除了弹窗, 还要把被点击的那个部门传出来的数据存起来
      // 作为桥梁传给弹窗
      this.node = node
    },
    async editDepts(node) {
      await this.$refs.addForm.getDepartDetail(node.id)
      this.showDialog = true
      this.node = node
      // 编辑窗口数据什么时候加载?
      // 在这个弹窗弹起的地方进行加载
      // 其实父组件是可以通过 ref 直接获取子组件
      // 并且调用子组件的函数
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
